import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import ClueView from '../views/ClueView.vue'
import CluePool from '../views/CluePool.vue'
import BusinessView from '../views/BusinessView.vue'
import PublicPoolView from '../views/PublicPoolView.vue'
import CustomerView from '../views/CustomerView.vue'
import CourseView from '../views/CourseView.vue'
import ActivityView from '../views/ActivityView.vue'
import DepartmentView from '../views/DepartmentView.vue'
import RoleView from '../views/RoleView.vue'
import UserView from '../views/UserView.vue'
import LogView from '../views/LogView.vue'
import { useAuthStore } from '@/stores/auth'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home' // 默认重定向到首页
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: {
        requiresAuth: false // 不需要认证
      }
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      meta: {
        requiresAuth: true // 需要认证
      }
    },
    {
      path: '/clue',
      name: 'clue',
      component: ClueView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/cluepool',
      name: 'cluepool',
      component: CluePool,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/business',
      name: 'business',
      component: BusinessView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/publicpool',
      name: 'publicpool',
      component: PublicPoolView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/customer',
      name: 'customer',
      component: CustomerView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/course',
      name: 'course',
      component: CourseView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/activity',
      name: 'activity',
      component: ActivityView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/department',
      name: 'department',
      component: DepartmentView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/role',
      name: 'role',
      component: RoleView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/user',
      name: 'user',
      component: UserView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/log',
      name: 'log',
      component: LogView,
      meta: {
        requiresAuth: true
      }
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 从本地存储恢复用户信息
  authStore.recoverUserInfo()
  
  // 判断页面是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 需要认证
    if (!authStore.isLoggedIn) {
      // 未登录，跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 保存用户想要访问的页面
      })
    } else {
      // 已登录，可以访问
      next()
    }
  } else {
    // 不需要认证的页面（如登录页）
    if (to.path === '/login' && authStore.isLoggedIn) {
      // 已登录，访问登录页时重定向到首页
      next('/home')
    } else {
      // 未登录或访问其他不需要认证的页面
      next()
    }
  }
})

export default router